package controller;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import logic.Item;
import logic.Shop;
import dao.ItemDao;

import org.springframework.ui.ModelMap;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.stereotype.Controller;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

@Controller
public class ShopController extends MultiActionController {

	private Shop shopService;
	private ItemDao itemDao; 

	public void setShopService(Shop shopService) {
		this.shopService = shopService;
	}
	
	static Logger log = Logger.getLogger(ShopController.class);
	
	public ModelAndView list(HttpServletRequest rq, HttpServletResponse rs) throws Exception {
		showParameters(rq);

		
//		// 상품 목록 정보를 취득
//		List<Item> itemList = this.shopService.getItemList();
//
//		// 모델 생성
//		Map<String, Object> model = new HashMap<String, Object>();
//		model.put("itemList", itemList);
//
//		// 반환값인 ModelAndView 인스턴스 생성
//		ModelAndView modelAndView = new ModelAndView();
//		modelAndView.setViewName("index");
//		modelAndView.addAllObjects(model);
		
				Map<String, Object> model = new HashMap<String, Object>();
		        List<Item> list = this.itemDao.findAll();
		        model.put("list", list);
		        
				// 반환값인 ModelAndView 인스턴스 생성
				ModelAndView modelAndView = new ModelAndView();
				modelAndView.setViewName("index");
				modelAndView.addAllObjects(model);

		return modelAndView;
	}
	
	public ModelAndView  detail(HttpServletRequest rq, HttpServletResponse rs) throws Exception {
		showParameters(rq);
		// 선택된 상품ID에서 상품 정보를 취득

		Item item = this.shopService.getItemByItemId(Integer.parseInt(rq.getParameter("itemId").toString()));

		// 모델 생성
		Map<String, Object> model = new HashMap<String, Object>();
		model.put("item", item);

		// 반환값인 ModelAndView 인스턴스를 생성
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.setViewName("detail");
		modelAndView.addAllObjects(model);

		return modelAndView;

	}

	/**
	 * Show Request Parameter
	 * 
	 * @param request
	 * @return void
	 * @throws Exception
	 */
	public void showParameters(HttpServletRequest request) {

		log.debug("###############################################################");
		log.debug("REQUEST  URL : " + request.getRequestURL());
		Enumeration<String> paramNames = request.getParameterNames();

		try {
			while (paramNames.hasMoreElements()) {
				String name = paramNames.nextElement().toString();
				String value = StringUtils.defaultIfEmpty(request.getParameter(name), "");

				log.debug("PARAM : " + name.toUpperCase() + "\t VALUE : " + value);

			}
			log.debug("###############################################################");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
/*	public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {

		// 상품 목록 정보를 취득
		List<Item> itemList = this.shopService.getItemList();

		// 모델 생성
		Map<String, Object> model = new HashMap<String, Object>();
		model.put("itemList", itemList);

		// 반환값인 ModelAndView 인스턴스 생성
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.setViewName("index");
		modelAndView.addAllObjects(model);

		return modelAndView;
	}*/
}